package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.framework.utils.DateUtils;
import light.head.model.FileInfo;
import light.head.model.SupplyAnnex;
import light.head.model.SupplyInfo;
@Service
public class SupplyInfoDAO extends BaseDAO<SupplyInfo, Serializable> {
	private static final String sql_findAnnex = "FROM SupplyAnnex sa WHERE sa.sic = ?";
	private static final String sql_queryBidList = "FROM SupplyInfo si WHERE si.status = 2 AND si.deleted = 0 ORDER BY si.bidTime DESC";
	private static final String sql_queryList = "FROM SupplyInfo si WHERE si.status = 1 AND si.deleted = 0 AND si.stopTime > curdate() ORDER BY si.code DESC";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM SupplyInfo si WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String title = map.get(Parm.TITLE);
			String oc = map.get(Parm.OC);
			String time1 = map.get(Parm.TIME1);
			String time2 = map.get(Parm.TIME2);
			String status = map.get(Parm.STATUS);
			String deleted = map.get(Parm.DELETED);
			String et = map.get(Parm.ET);
			if (oc != "" && isNotNull(oc)) {
				hql.append(" AND si.oc = ? ");
				list.add(Integer.parseInt(oc));
			}
			if (title != "" && isNotNull(title)) {
				hql.append(" AND si.name LIKE ? ");
				list.add("%" + title + "%");
			}
			if (time1 != "" && isNotNull(time1)) {
				hql.append(" AND si.ct >= ? ");
				list.add(DateUtils.getDateByStr(time1, "yyyy-MM-dd"));
			}
			if (time2 != "" && isNotNull(time2)) {
				hql.append(" AND si.ct <= ?");
				list.add(DateUtils.getDateByStr(time2, "yyyy-MM-dd"));
			}
			if (status != "" && isNotNull(status)) {
				hql.append(" AND si.status = ?");
				list.add(Byte.parseByte(status));
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND si.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
			if (isNotNull(et)) {
				hql.append(" AND si.stopTime > ?");
				list.add(DateUtils.getDateByStr(et, "yyyy-MM-dd"));
			}
			
		}
		hql.append(" ORDER BY si.code DESC");

		return findToPageBean(hql.toString(), pageBean, list);
	}

	/**
	 * 根据拼接好的文件编号序列获取对应的文件列表
	 */
	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}

	public List<SupplyAnnex> findAnnexByCode(Integer code) {
		return super.find(sql_findAnnex, code);
	}

	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			if(o != null){
				sql.append("?,");
				parm.add(o);
			}
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND sa.code NOT IN(")
					.deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM SupplyAnnex sa WHERE sa.sic = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	
	public List<SupplyInfo> getBidList(){
		return super.find(sql_queryBidList);
	}
	
	public List<SupplyInfo> getList(){
		return super.find(sql_queryList);
	}
}
